<template>
  <el-dialog
    v-model="dialogShow"
    :close-on-click-modal="false"
    :destroy-on-close="false"
    custom-class="mydialog"
    width="1000px"
    title="作废配送服务"
    @close="close"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :label-width="formLabelWidth"
      size="small"
      :rules="formRules"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="配送服务编号：">
            <div>{{ serviceDetail.distributeServeNo }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配送产品信息：">
            <div>{{ getProductName(serviceDetail) }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配送费：">
            <div>
              <div v-if="serviceDetail.computeMode==='固定价格'">
                <span>{{ serviceDetail.distributePrice }}元</span>
                <span v-if="serviceDetail.sellUnit">/{{ getLabel('sell_unit',serviceDetail.sellUnit) }}</span>
              </div>
              <div v-if="serviceDetail.computeMode==='终端价百分比'">
                <span>{{ serviceDetail.distributePrice }}%</span>
                <span>终端价</span>
              </div>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="配送地区：">
            <area-list-view :areas="serviceDetail.region" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关联配送合同：">
            <div>{{ serviceDetail.contractNumber||'无' }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务结束日期：">
            <div>{{ dateFormat('yyyy-mm-dd HH:MM',serviceDetail.expireTime) }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="摘要信息：">
            <div>{{ serviceDetail.distributeAbstract||'无' }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="作废原因："
            prop="invalidateReason"
          >
            <el-input
              v-model="formData.invalidateReason"
              type="textarea"
              placeholder="请输入作废原因"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div
        class="dialog-footer"
        style="display:flex;justify-content:center;align-items:center;"
      >
        <el-button
          class="border-btn"
          size="small"
          @click="close"
        >取消</el-button>
        <el-button
          class="main-btn"
          size="small"
          type="primary"
          @click="confirm"
        >提交审核</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {
  defineComponent,
  getCurrentInstance,
  inject,
  reactive,
  ref,
  toRefs,
} from 'vue'

export default defineComponent({
  name: 'ObsoleteService',
  emits: ['onsuccess'],
  setup(props, context) {
    const api = inject('api')
    const currentInstance = getCurrentInstance()
    const formRef = ref(null)
    var reactiveData = reactive({
      addOrChange: 'add',
      serviceDetail: {},
      terminalOptions: [],
      formData: {
        id: 0,
        invalidateReason: '',
      },
    })
    var formRules = reactive({
      invalidateReason: [
        { required: true, message: '请输入作废原因', trigger: 'blur' },
      ],
    })
    var formLabelWidth = ref(140)
    var dialogShow = ref(false)
    const open = (rowData) => {
      reactiveData.serviceDetail = rowData
      reactiveData.formData.id = rowData.id
      dialogShow.value = true
    }
    const close = () => {
      dialogShow.value = false
      formRef.value.resetFields()
    }
    const confirm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          console.log(525454)
          api.distributors
            .DISTRIBUTORS_SERVICE_OBSOLETE(reactiveData.formData)
            .then((res) => {
              if (res.code === 0) {
                context.emit('onsuccess')
                currentInstance.proxy.customMessage({
                  type: 'success',
                  message:
                    '<配送服务作废审核>已提交，可在[流程审批-我发起的]查看审核进程。',
                })
                close()
              } else {
                currentInstance.proxy.customMessage({
                  type: 'error',
                  message: res.msg,
                })
              }
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
    return {
      formRef,
      ...toRefs(reactiveData),
      formRules,
      formLabelWidth,
      dialogShow,
      open,
      close,
      confirm,
    }
  },
})
</script>
